{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa548dee-da72-464a-9ca0-d71c14219812",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "欢迎使用学生成绩管理系统\n",
      "==============================\n",
      "1. 记录学生成绩\n",
      "2. 查询学生成绩\n",
      "3. 统计课程成绩\n",
      "4. 退出系统\n",
      "==============================\n"
     ]
    }
   ],
   "source": [
    "class StudentGradeSystem:\n",
    "    def __init__(self):\n",
    "        self.students = []  # 存储学生成绩数据\n",
    "        \n",
    "    def display_menu(self):\n",
    "        \"\"\"显示主菜单\"\"\"\n",
    "        print(\"\\n欢迎使用学生成绩管理系统\")\n",
    "        print(\"=\" * 30)\n",
    "        print(\"1. 记录学生成绩\")\n",
    "        print(\"2. 查询学生成绩\")\n",
    "        print(\"3. 统计课程成绩\")\n",
    "        print(\"4. 退出系统\")\n",
    "        print(\"=\" * 30)\n",
    "        \n",
    "    def add_student_grade(self):\n",
    "        \"\"\"记录学生成绩\"\"\"\n",
    "        print(\"\\n—— 记录学生成绩 ——\")\n",
    "        while True:\n",
    "            # 输入学生姓名\n",
    "            name = input(\"请输入学生姓名：\").strip()\n",
    "            while not name:\n",
    "                print(\"姓名不能为空，请重新输入！\")\n",
    "                name = input(\"请输入学生姓名：\").strip()\n",
    "            \n",
    "            # 输入学号并检查是否重复\n",
    "            student_id = input(\"请输入学生学号：\").strip()\n",
    "            while not student_id:\n",
    "                print(\"学号不能为空，请重新输入！\")\n",
    "                student_id = input(\"请输入学生学号：\").strip()\n",
    "            \n",
    "            if any(s['学号'] == student_id for s in self.students):\n",
    "                print(f\"错误：学号 {student_id} 已存在！\")\n",
    "                continue\n",
    "            \n",
    "            # 输入课程名称\n",
    "            course = input(\"请输入课程名称：\").strip()\n",
    "            while not course:\n",
    "                print(\"课程名称不能为空，请重新输入！\")\n",
    "                course = input(\"请输入课程名称：\").strip()\n",
    "            \n",
    "            # 输入成绩并验证\n",
    "            while True:\n",
    "                try:\n",
    "                    grade = float(input(\"请输入成绩（0-100）：\"))\n",
    "                    if 0 <= grade <= 100:\n",
    "                        break\n",
    "                    print(\"成绩必须在0-100之间，请重新输入！\")\n",
    "                except ValueError:\n",
    "                    print(\"请输入有效的数字！\")\n",
    "            \n",
    "            self.students.append({\n",
    "                '姓名': name,\n",
    "                '学号': student_id,\n",
    "                '课程': course,\n",
    "                '成绩': grade\n",
    "            })\n",
    "            print(f\"成功：{name}的{course}成绩{grade}已记录！\")\n",
    "            \n",
    "            choice = input(\"是否继续添加？(y/n): \").lower()\n",
    "            if choice != 'y':\n",
    "                break\n",
    "                \n",
    "    def query_grades(self):\n",
    "        \"\"\"查询学生成绩\"\"\"\n",
    "        if not self.students:\n",
    "            print(\"当前没有学生成绩记录！\")\n",
    "            return\n",
    "            \n",
    "        print(\"\\n—— 查询学生成绩 ——\")\n",
    "        print(\"1. 按学生姓名查询\")\n",
    "        print(\"2. 按学生学号查询\")\n",
    "        print(\"3. 按课程名称查询\")\n",
    "        \n",
    "        # 获取查询选项\n",
    "        while True:\n",
    "            try:\n",
    "                option = input(\"请输入选项序号：\").strip()\n",
    "                if option in ['1', '2', '3']:\n",
    "                    break\n",
    "                print(\"请输入1-3之间的数字！\")\n",
    "            except ValueError:\n",
    "                print(\"请输入有效的数字！\")\n",
    "        \n",
    "        if option == '1':\n",
    "            name = input(\"请输入学生姓名：\").strip()\n",
    "            results = [s for s in self.students if s['姓名'] == name]\n",
    "            if not results:\n",
    "                print(f\"没有找到姓名为 {name} 的学生记录！\")\n",
    "            else:\n",
    "                for record in results:\n",
    "                    print(f\"姓名：{record['姓名']}，学号：{record['学号']}，课程：{record['课程']}，成绩：{record['成绩']}\")\n",
    "                    \n",
    "        elif option == '2':\n",
    "            student_id = input(\"请输入学生学号：\").strip()\n",
    "            results = [s for s in self.students if s['学号'] == student_id]\n",
    "            if not results:\n",
    "                print(f\"没有找到学号为 {student_id} 的学生记录！\")\n",
    "            else:\n",
    "                for record in results:\n",
    "                    print(f\"姓名：{record['姓名']}，学号：{record['学号']}，课程：{record['课程']}，成绩：{record['成绩']}\")\n",
    "                    \n",
    "        elif option == '3':\n",
    "            course = input(\"请输入课程名称：\").strip()\n",
    "            results = [s for s in self.students if s['课程'] == course]\n",
    "            if not results:\n",
    "                print(f\"没有找到课程为 {course} 的成绩记录！\")\n",
    "            else:\n",
    "                for record in results:\n",
    "                    print(f\"姓名：{record['姓名']}，学号：{record['学号']}，成绩：{record['成绩']}\")\n",
    "    \n",
    "    def course_statistics(self):\n",
    "        \"\"\"统计课程成绩\"\"\"\n",
    "        if not self.students:\n",
    "            print(\"当前没有学生成绩记录！\")\n",
    "            return\n",
    "            \n",
    "        print(\"\\n【统计课程成绩】\")\n",
    "        print(\"--- 统计课程成绩 ---\")\n",
    "        course = input(\"请输入课程名称：\").strip()\n",
    "        \n",
    "        # 获取该课程所有成绩\n",
    "        course_grades = [s['成绩'] for s in self.students if s['课程'] == course]\n",
    "        \n",
    "        if not course_grades:\n",
    "            print(f\"没有找到课程为 {course} 的成绩记录！\")\n",
    "            return\n",
    "        \n",
    "        # 计算统计数据\n",
    "        avg = sum(course_grades) / len(course_grades)\n",
    "        max_grade = max(course_grades)\n",
    "        min_grade = min(course_grades)\n",
    "        \n",
    "        # 按照图片格式输出结果\n",
    "        print(f\"课程：{course}\")\n",
    "        print(f\"平均分：{avg:.2f}\")\n",
    "        print(f\"最高分：{max_grade:.1f}\")\n",
    "        print(f\"最低分：{min_grade:.1f}\")\n",
    "        print()\n",
    "                    \n",
    "    def run(self):\n",
    "        \"\"\"运行系统\"\"\"\n",
    "        while True:\n",
    "            self.display_menu()\n",
    "            \n",
    "            choice = input(\"请输入选项序号：\").strip()\n",
    "            \n",
    "            if choice == '1':\n",
    "                self.add_student_grade()\n",
    "            elif choice == '2':\n",
    "                self.query_grades()\n",
    "            elif choice == '3':\n",
    "                self.course_statistics()\n",
    "            elif choice == '4':\n",
    "                print(\"\\n感谢使用学生成绩管理系统，再见！\")\n",
    "                break\n",
    "            else:\n",
    "                print(\"无效的选项，请重新输入！\")\n",
    "\n",
    "# 启动系统\n",
    "if __name__ == \"__main__\":\n",
    "    system = StudentGradeSystem()\n",
    "    system.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "86e3ea94-b919-445a-a7c5-7872a2f0b249",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
